package scau.erps.view.dashboard;

import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import scau.erps.app.Erps;
import scau.erps.entity.Score;
import scau.erps.service.ScoreService;
import scau.erps.service.impl.ScoreServiceImpl;

/**
 * 综合排名柱状图 
 * @author outsider

 */
public class RankChart {
	/**
	 * 得到图片
	 * 
	 */
	public static JFreeChart getChart() {
		JFreeChart chart = createChart(createDataset());
		return chart;
	}

	/**
	 * 数据
	 */
	private static CategoryDataset createDataset() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        ScoreService scoreService=Erps.getIocInstance().get(ScoreServiceImpl.class);
        List<Score> list=scoreService.getScore(0);
        List<Score> listSort=new ArrayList();
		int size=list.size();
		for (int i=0;i<size;i++){
			int max=-9999,k=0;
			for (int j=0;j<list.size();j++)
				if (list.get(j).getScore()>max){
					max=list.get(j).getScore();
					k=j;
				}
			listSort.add(list.get(k));
			list.remove(k);
		}
		for (Score score:listSort){
			dataset.setValue(score.getScore(), "", "第"+score.getTeam_Id()+"组");
		}
		return dataset;
	}

	/**
	 * 绘制折线
	 */
	private static JFreeChart createChart(CategoryDataset dataset) {
		JFreeChart jfreeChart = ChartFactory.createBarChart3D("公司综合排名","小组", "得分", dataset, PlotOrientation.VERTICAL, true, false,false);
		jfreeChart.setTitle(new TextTitle("公司综合排名", new Font("宋体", Font.BOLD + Font.ITALIC, 20)));
		CategoryPlot plot = (CategoryPlot) jfreeChart.getPlot();
		CategoryAxis categoryAxis = plot.getDomainAxis();
		categoryAxis.setLabelFont(new Font("仿宋", Font.ROMAN_BASELINE, 12));
		return jfreeChart;
	}
}
